/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Titled 互斥锁保护的偶数生成器
 * @Description *
 * @Package concurrent.mthread.bf
 * @Since 2021/8/12 5:08 下午
 */
package concurrent.mthread.bf;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MutexEvenGenerator extends IntGenerator {

    private int currentEvenValue = 0;

    private Lock lock = new ReentrantLock();

    @Override
    public int next() {
        lock.lock();
        try {
            ++currentEvenValue;//Synchronized protected
            Thread.yield();
            ++currentEvenValue;

            System.out.println(Thread.currentThread().getName() + "| currentEvenValue is :" + currentEvenValue);
            return currentEvenValue;
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        EvenChecker.test(new MutexEvenGenerator());
    }
}
